<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #canvas {
      box-shadow: 2px 4px 6px #eee;
    }
  </style>
</head>
<body>
  <canvas id="canvas"></canvas>
</body>
<script src="../script/utils.js"></script>
<script src="../components/Ball.js"></script> 
<script>
  const canvas = document.getElementById('canvas')
  const ctx = canvas.getContext('2d')
  
  const W = canvas.width = 800
  const H = canvas.height = 600
  
  // let mouse = C.getOffset(canvas)

  let angle = C.toRad(30), speed = C.rp([30, 50]), friction = 0.9
  const ball = new Ball({
      x:50,
      y: 50,
      r: 30,
      fillStyle: C.createColor()
    }).render(ctx)

  function move() {
    window.requestAnimationFrame(move)
    ctx.clearRect(0, 0, W, H)
    
    // speed = speed > friction ? speed - friction : 0
    if (Math.abs(speed) > 0.01) {
      speed *= friction

      let vx = Math.cos(angle) * speed
      let vy = Math.sin(angle) * speed

      ball.x += vx
      ball.y += vy
      console.log(speed)
    }
    ball.render(ctx)
  }
  move()
</script>
</html>